#include "ThingsBoard.h"
#include <ESP8266WiFi.h>

int sensor= 12;  // Digital pin D6
int Status = 13;  // Digital pin D7
int timeOut = 0;
int isled = 0;
int isperson = 0;

#define WIFI_AP             "ybh_001"
#define WIFI_PASSWORD       "********"

// See https://thingsboard.io/docs/getting-started-guides/helloworld/
// to understand how to obtain an access token
#define TOKEN               "1OTn************"
#define THINGSBOARD_SERVER  "39.105.185.205"
#define PORT          1884          //"yvHXKmV87ecYa6Bx8uHD"
// Baud rate for debug serial
#define SERIAL_DEBUG_BAUD   115200

// Initialize ThingsBoard client
WiFiClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
// the Wifi radio's status
int status = WL_IDLE_STATUS;

void setup() {
  pinMode(sensor, INPUT);   // declare sensor as input
  pinMode(Status, OUTPUT);  // declare LED as output
  // initialize serial for debugging
  Serial.begin(SERIAL_DEBUG_BAUD);
  WiFi.begin(WIFI_AP, WIFI_PASSWORD);
  pinMode(LED_BUILTIN, OUTPUT);
  InitWiFi();
}

void loop() {
  if(isled==0){
    digitalWrite(LED_BUILTIN, LOW);  // Turn the LED off by making the voltage HIGH
    isled=1;
  }else{
    digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
    isled=0;
  }
  delay(1000);
  
  if (WiFi.status() != WL_CONNECTED) {
    reconnect();
  }
  Serial.println("444444444");
  Serial.println(WiFi.localIP());
  if (!tb.connected()) {
    // Connect to the ThingsBoard
    Serial.print("Connecting to: ");
    Serial.print(THINGSBOARD_SERVER);
    Serial.print(" with token ");
    Serial.println(TOKEN);
    if (!tb.connect(THINGSBOARD_SERVER, TOKEN, PORT)) {
      Serial.println("Failed to connect");
      return;
    }
  }

  Serial.println("Sending data...");

  // Uploads new telemetry to ThingsBoard using MQTT.
  // See https://thingsboard.io/docs/reference/mqtt-api/#telemetry-upload-api
  // for more details
  long state = digitalRead(sensor);
  Serial.println(state);
  if (state == HIGH) {
    isperson = 1;
    tb.sendTelemetryInt("online", 100);
    digitalWrite(LED_BUILTIN, LOW);  // Turn the LED off by making the voltage HIGH
    timeOut = 0;
  }else { 
    timeOut++;
	if(isperson==1){
		tb.sendTelemetryInt("online", 100);
		digitalWrite(LED_BUILTIN, LOW);
	}
  }
  Serial.println(timeOut);
  if(timeOut >= 30 && isperson == 1){
	 isperson = 0;
     Serial.println("Motion absent!");
     tb.sendTelemetryInt("online", 1);
     digitalWrite(LED_BUILTIN, HIGH);   // Turn the LED on (Note that LOW is the voltage level
     timeOut=0;    
  }
  
  if(isperson == 0){
	  tb.sendTelemetryInt("online", 1);
	  digitalWrite(LED_BUILTIN, HIGH);
  }
  tb.loop();
}

void InitWiFi()
{
  Serial.println("Connecting to AP ...");
  // attempt to connect to WiFi network

  WiFi.begin(WIFI_AP, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to AP");
}

void reconnect() {
  // Loop until we're reconnected
  status = WiFi.status();
  if ( status != WL_CONNECTED) {
    WiFi.begin(WIFI_AP, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("Connected to AP");
  }
}
